1
/**************************** Module Header ********************************\
2 * Module Name: CodeManager.cs
3 * Project: CSASPNETHighlightCodeInPage
4 * Copyright (c) Microsoft Corporation
6 * Sometimes we input code like C# or HTML in our post and we need these code
7 * to be highlighted for a better reading experience.This project illustrates how
8 * to highlight the code in a page.
10 * In this file,we use a Hashtable variable to store the different languages of
11 * code and their related regular expressions with matching options.Then add the
12 * style object to the matching string of code.
14 * This source is subject to the Microsoft Public License.
15 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
16 * All other rights reserved.
18 \***************************************************************************/
20 using System
.Collections
;
21 using System
.Text
.RegularExpressions
;
24 namespace CSASPNETHighlightCodeInPage
27 /// The structure of style string and regular expressions.
29 public struct RegexStruct
31 public string styleObject
;
36 /// Matching string and style object.
41 /// Store the matching string collection.
43 private ArrayList _regexStructList
= new ArrayList();
47 /// <param name="styleObject">Style object</param>
48 /// <param name="reg">Regular expressions</param>
49 /// <param name="regOption">Matching options</param>
50 public void Add(string styleObject
, string reg
, RegexOptions regOption
)
52 RegexStruct regexStruct
= new RegexStruct();
53 regexStruct
.styleObject
= styleObject
;
54 regexStruct
.regex
= new Regex(reg
, regOption
| RegexOptions
.Compiled
);
55 _regexStructList
.Add(regexStruct
);
58 /// Return the matching string collection ,read only.
60 public ArrayList regexStructList
62 get { return _regexStructList; }
67 /// Highlight the code operation.
69 public class CodeManager
72 /// Initialize the Hashtable variable which used to store the
73 /// the different languages of code and their related regular
74 /// expressions with matching options.
76 /// <returns></returns>
77 public static Hashtable
Init()
79 Hashtable hashTable
= new Hashtable();
80 RegExp regExp
= new RegExp();
82 // Add the information of VBScript language to a Hashtable variable.
83 #region VBScript language
84 regExp
.Add("str", "\"([^\"\\n]*?)\"", RegexOptions
.None
);
85 regExp
.Add("note", "'[^\r\n]*", RegexOptions
.None
);
86 regExp
.Add("kw", @"\b(elseif|if|then|else|select|case|end|for|while"
87 + "|wend|do|loop|until|abs|sgn|hex|oct|sqr|int|fix|round"
88 + "|log|split|cint|sin|cos|tan|len|mid|left|right|lcase|ucase"
89 + "|trim|ltrim|rtrim|replace|instr|instrrev|space|string"
90 + "|strreverse|cstr|clng|cbool|cdate|csng|cdbl|date|time|now"
91 + "|dateadd|datediff|dateserial|datevalue|year|month|day|hour"
92 + "|minute|second|timer|timeserial|timevalue|weekday|monthname"
93 + "|array|asc|chr|filter|inputbox|join|msgbox|lbound|ubound"
94 + "|redim|randomize|rnd|isempty|mod|execute|not|and|or|xor"
96 + @"|class(?!\s*=))\b", RegexOptions
.IgnoreCase
);
97 hashTable
.Add("vbs", regExp
);
100 // Add the information of JavaScript language to a Hashtable variable.
101 #region JavaScript language
102 regExp
= new RegExp();
103 regExp
.Add("str", "\"[^\"\\n]*\"|'[^'\\n]*'", RegexOptions
.None
);
104 regExp
.Add("note", @"\/\/[^\n\r]*|\/\*[\s\S]*?\*\/", RegexOptions
.None
);
105 regExp
.Add("kw", @"\b(break|delete|function|return|typeof|case|do|if"
106 + "|switch|var|catch|else|in|this|void|continue|false|nstanceof"
107 + "|throw|while|debugger|finally|new|true|with|default|for|null"
108 + "|try|abstract|double|goto|native|static|boolean|enum|implements"
109 + "|package|super|byte|export|import|private|synchronized|char"
110 + "|extends|int|protected|throws|final|interface|public|transient"
111 + "|const|float|long|short|volatile"
112 + @"|class(?!\s*=))\b", RegexOptions
.None
);
113 hashTable
.Add("js", regExp
);
116 // Add the information of SqlServer language to a Hashtable variable.
117 #region SqlServer language
118 regExp
= new RegExp();
119 regExp
.Add("sqlstr", "'([^'\\n]*?)*'", RegexOptions
.None
);
120 regExp
.Add("note", @"--[^\n\r]*|\/\*[\s\S]*?\*\/", RegexOptions
.None
);
121 regExp
.Add("sqlconnect", @"\b(all|and|between|cross|exists|in|join|like"
122 + "|not|null|outer|or)\b", RegexOptions
.IgnoreCase
);
123 regExp
.Add("sqlfunc", @"\b(avg|case|checksum|current_timestamp|day|left"
124 + "|month|replace|year)\b", RegexOptions
.IgnoreCase
);
125 regExp
.Add("kw", @"\b(action|add|alter|after|as|asc|bigint|bit|binary|by"
126 + "|cascade|char|character|check|column|columns|constraint|create"
127 + "|current_date|current_time|database|date|datetime|dec|decimal"
128 + "|default|delete|desc|distinct|double|drop|end|else|escape|file"
129 + "|first|float|foreign|from|for|full|function|global|grant|group"
130 + "|having|hour|ignore|index|inner|insert|int|integer|into|if|is"
131 + "|key|kill|load|local|max|minute|modify|numeric|no|on|option|order"
132 + "|partial|password|precision|primary|procedure|privileges"
133 + "|read|real|references|restrict|returns|revoke|rows|second|select"
134 + "|set|shutdown|smallint|table|temporary|text|then|time"
135 + "|timestamp|tinyint|to|use|unique|update|values|varchar|varying"
136 + @"|varbinary|with|when|where)\b", RegexOptions
.IgnoreCase
);
137 hashTable
.Add("sql", regExp
);
140 // Add the information of C# language to a Hashtable variable.
142 regExp
= new RegExp();
143 regExp
.Add("str", "\"[^\"\\n]*\"", RegexOptions
.None
);
144 regExp
.Add("note", @"\/\/[^\n\r]*|\/\*[\s\S]*?\*\/", RegexOptions
.None
);
145 regExp
.Add("Var", @"(?<=\bclass\s+)([_a-z][_a-z0-9]*)(?=\s*[\{:])"
146 + @"|(?<=\=\s*new\s+)([a-z_][a-z0-9_]*)(?=\s*\()"
147 + @"|([a-z][a-z0-9_]*)(?=\s+[a-z_][a-z0-9_]*\s*=\s*new)",
148 RegexOptions
.IgnoreCase
);
149 regExp
.Add("kw", @"\b(partial|abstract|event|get|set|value|new|struct|as"
150 + "|null|switch|base|object|this|bool|false|operator|throw|break"
151 + "|finally|out|byte|fixed|override|try|case|float|params|typeof"
152 + "|catch|for|private|uint|char|foreach|protected|ulong|checked"
153 + "|goto|public|unchecked|if|readonly|unsafe|const|implicit|ref"
154 + "|ushort|continue|in|return|using|decimal|int|sbyte|virtual"
155 + "|default|interface|sealed|volatile|delegate|internal|short|void"
156 + "|do|is|sizeof|while|double|lock|stackalloc|else|long|static"
157 + @"|enum|string|namespace|region|endregion|class(?!\s*=))\b",
159 regExp
.Add("kwG", @"\b(EventArgs|Page|Label|TextBox|CheckBox|DropDownList"
160 + @"|Control|Button|DayRenderEventArgs|Color(?!\s*=))\b",
162 hashTable
.Add("cs", regExp
);
165 // Add the information of VB.NET language to a Hashtable variable.
166 #region VB.NET language
167 regExp
= new RegExp();
168 regExp
.Add("str", "\"[^\"\\n]*\"", RegexOptions
.None
);
169 regExp
.Add("note", @"'[^\n\r]*", RegexOptions
.None
);
170 regExp
.Add("Var", @"(?<=\bclass\s+)([_a-z][_a-z0-9]*)(?=\s*[\{:])"
171 + @"|(?<=\=\s*new\s+)([a-z_][a-z0-9_]*)(?=\s*\()"
172 + @"|([a-z][a-z0-9_]*)(?=\s+[a-z_][a-z0-9_]*\s*=\s*new)",
173 RegexOptions
.IgnoreCase
);
174 regExp
.Add("kw", @"\b(AddHandler|AddressOf|AndAlso|Alias|And|Ansi|As"
175 + "|Assembly|Auto|Boolean|Class|CLng|CObj|Const|Char|CInt|CDbl"
176 + "|ByRef|Byte|ByVal|Call|Case|Catch|CBool|CByte|CChar|CDate|CDec"
177 + "|CShort|CSng|CStr|CType|Date|Decimal|Declare|Default|Delegate"
178 + "|Dim|DirectCast|Do|Double|Each|Else|ElseIf|End|Handles|If"
179 + "|Enum|Erase|Error|Event|Exit|False|Finally|For|Friend|Function"
180 + "|Get|GetType|GoTo|Let|Lib|Like|Long|Loop|Me|Mod|Module|Nothing"
181 + "|Implements|Imports|In|Inherits|Integer|Interface|Is|Public"
182 + "|MustInherit|MustOverride|MyBase|MyClass|Namespace|New|Next|Not"
183 + "|NotInheritable|NotOverridable|Object|On|Option|Optional|Or|OrElse"
184 + "|Overloads|Overridable|Overrides|ParamArray|Preserve|Private"
185 + "|RaiseEvent|ReadOnly|ReDim|RemoveHandler|Resume|Return|Property"
186 + "|Select|Set|Shadows|Shared|Short|Single|Static|Step|Stop|String"
187 + "|Structure|Sub|SyncLock|Then|Throw|Protected|TypeOf|Unicode|Try"
188 + "|To|True|Until|Variant|When|While|With|WithEvents"
189 + @"|WriteOnly|Xor(?!\s*=))\b", RegexOptions
.None
);
190 regExp
.Add("kwG", @"\b(EventArgs|Page|Label|TextBox|CheckBox|DropDownList"
191 + @"|Control|Button|Nullable|DayRenderEventArgs|Color(?!\s*=))\b",
193 hashTable
.Add("vb", regExp
);
196 // Add the information of CCS syntax to a Hashtable variable.
198 regExp
= new RegExp();
199 regExp
.Add("note", @"\/\*[\s\S]*?\*\/", RegexOptions
.None
);
200 regExp
.Add("str", @"([\s\S]+)", RegexOptions
.None
);
201 regExp
.Add("kw", @"(\{[^\}]+\})", RegexOptions
.None
);
202 regExp
.Add("sqlstr", @"([a-z\-]+(?=\s*:))", RegexOptions
.IgnoreCase
);
203 regExp
.Add("black", @"([\{\}])", RegexOptions
.None
);
204 hashTable
.Add("css", regExp
);
207 // Add the information of HTML language to a Hashtable variable.
208 #region HTML language
209 regExp
= new RegExp();
210 regExp
.Add("", "<%@\\s*page[\\s\\S]*?language=['\"](.*?)[\"']",
211 RegexOptions
.IgnoreCase
);
212 regExp
.Add("", @"<!--([\s\S]*?)-->", RegexOptions
.None
);
213 regExp
.Add("", @"(<script[^>]*>)([\s\S]*?)<\/script>",
214 RegexOptions
.IgnoreCase
);
215 regExp
.Add("", @"<%(?!@)([\s\S]*?)%>", RegexOptions
.None
);
216 regExp
.Add("", @"<\?php\b([\s\S]*?)\?>", RegexOptions
.IgnoreCase
);
217 regExp
.Add("", @"(<style[^>]*>)([\s\S]*?)<\/style>",
218 RegexOptions
.IgnoreCase
);
219 regExp
.Add("", @"&([a-z]+;)", RegexOptions
.None
);
220 regExp
.Add("", @"'.*?'", RegexOptions
.None
);
221 regExp
.Add("", "\".*?\"", RegexOptions
.None
);
222 regExp
.Add("", @"<([^>]+)>", RegexOptions
.None
);
223 hashTable
.Add("html", regExp
);
229 /// Replace the brackets which contained in quotes or single quotes.
231 /// <param name="m"></param>
232 /// <returns></returns>
233 private static string NoteBrackets(Match m
)
235 return "<span class='gray'>"
236 + m
.Groups
[0].Value
.Replace("<", "<").Replace(">", ">")
241 /// Replace the brackets.
243 /// <param name="m"></param>
244 /// <returns></returns>
245 private static string RetrieveBrackets(Match m
)
247 return m
.Groups
[0].Value
.Replace("<", "<").Replace(">", ">");
252 /// Highlight the HTML code
254 /// <param name="codeString">The string of code</param>
255 /// <param name="hashTable">The regular expressions collection</param>
256 /// <returns>Highlighted code</returns>
257 public static string HighlightHTMLCode(string codeString
,
261 RegExp regExp
= (RegExp
)hashTable
["html"];
262 Regex regex
= ((RegexStruct
)regExp
.regexStructList
[0]).regex
,
263 htmlR
= ((RegexStruct
)regExp
.regexStructList
[9]).regex
;
264 Match match
= regex
.Match(codeString
);
266 ArrayList note
= new ArrayList(),
267 vb
= new ArrayList(),
268 js
= new ArrayList(),
269 cs
= new ArrayList(),
270 css
= new ArrayList();
273 // Get the default language of page.
274 if (match
.Groups
[1].Value
.Trim() != "") lang
275 = match
.Groups
[1].Value
.ToUpper().Trim();
276 if (lang
!= "C#") lang
= "VB";
278 #region Characters replaced
279 codeString
= codeString
.Replace("\\\"", "__CharactersQuotes__")
280 .Replace("\\'", "__CharactersSingleQuote__");
284 regex
= ((RegexStruct
)regExp
.regexStructList
[2]).regex
;
285 mc
= regex
.Matches(codeString
);
286 foreach (Match m
in mc
)
288 if (m
.Groups
[1].Value
.ToLower().IndexOf("runat") == -1)
291 if (m
.Groups
[1].Value
.ToLower().
292 IndexOf("vbscript") == -1)
294 blockIndex
= js
.Count
;
295 js
.Add(htmlR
.Replace(m
.Groups
[1].Value
, HTMLEval
) +
296 (m
.Groups
[2].Value
.Trim() != "" ?
297 HighlightCode(m
.Groups
[2].Value
, "js",
298 (RegExp
)hashTable
["js"]) : "")
299 + "<span class=\"kw\"></"
300 + "<span class=\"str\">script</span>></span> ");
301 codeString
= regex
.Replace(codeString
,
302 "__JS" + blockIndex
+ "__", 1);
307 blockIndex
= vb
.Count
;
308 vb
.Add(htmlR
.Replace(m
.Groups
[1].Value
, HTMLEval
) +
309 (m
.Groups
[2].Value
.Trim() != "" ?
310 HighlightCode(m
.Groups
[2].Value
, "vbs",
311 (RegExp
)hashTable
["vbs"]) : "")
312 + "<span class=\"kw\"></"
313 + "<span class=\"str\">script</span>></span> ");
314 codeString
= regex
.Replace(codeString
,
315 "__VB" + blockIndex
+ "__", 1);
323 if (m
.Groups
[1].Value
.ToLower().IndexOf("vb") == -1)
325 blockIndex
= cs
.Count
;
326 cs
.Add(htmlR
.Replace(m
.Groups
[1].Value
, HTMLEval
) +
327 (m
.Groups
[2].Value
.Trim() != "" ?
328 HighlightCode(m
.Groups
[2].Value
, "cs",
329 (RegExp
)hashTable
["cs"]) : "")
330 + "<span class=\"kw\"></"
331 + "<span class=\"str\">script</span>></span> ");
332 codeString
= regex
.Replace(codeString
,
333 "__C#" + blockIndex
+ "__", 1);
337 // VBScript language tags.
338 blockIndex
= vb
.Count
;
339 vb
.Add(htmlR
.Replace(m
.Groups
[1].Value
, HTMLEval
) +
340 (m
.Groups
[2].Value
.Trim() != "" ?
341 HighlightCode(m
.Groups
[2].Value
, "vbs",
342 (RegExp
)hashTable
["vbs"]) : "")
343 + "<span class=\"kw\"></"
344 + "<span class=\"str\">script</span>></span> ");
345 codeString
= regex
.Replace(codeString
,
346 "__VB" + blockIndex
+ "__", 1);
351 if (m
.Groups
[1].Value
.ToLower().IndexOf("c#") != -1)
353 blockIndex
= cs
.Count
;
354 cs
.Add(htmlR
.Replace(m
.Groups
[1].Value
, HTMLEval
) +
355 (m
.Groups
[2].Value
.Trim() != "" ?
356 HighlightCode(m
.Groups
[2].Value
, "cs",
357 (RegExp
)hashTable
["cs"]) : "")
358 + "<span class=\"kw\"></"
359 + "<span class=\"str\">script</span>></span> ");
360 codeString
= regex
.Replace(codeString
,
361 "__C#" + blockIndex
+ "__", 1);
365 blockIndex
= vb
.Count
;
366 vb
.Add(htmlR
.Replace(m
.Groups
[1].Value
, HTMLEval
) +
367 (m
.Groups
[2].Value
.Trim() != "" ?
368 HighlightCode(m
.Groups
[2].Value
, "vbs",
369 (RegExp
)hashTable
["vbs"]) : "")
370 + "<span class=\"kw\"></"
371 + "<span class=\"str\">script</span>></span> ");
372 codeString
= regex
.Replace(codeString
,
373 "__VB" + blockIndex
+ "__", 1);
382 regex
= ((RegexStruct
)regExp
.regexStructList
[5]).regex
;
383 mc
= regex
.Matches(codeString
);
385 foreach (Match m
in mc
)
387 css
.Add(htmlR
.Replace(m
.Groups
[1].Value
, HTMLEval
) +
388 (m
.Groups
[2].Value
.Trim() != "" ?
389 HighlightCode(m
.Groups
[2].Value
, "css",
390 (RegExp
)hashTable
["css"]) : "")
391 + "<span class=\"kw\"></"
392 + "<span class=\"str\">style</span>></span> ");
393 codeString
= regex
.Replace(codeString
,
394 "__CSS" + blockIndex
+ "__", 1);
400 regex
= ((RegexStruct
)regExp
.regexStructList
[1]).regex
;
401 mc
= regex
.Matches(codeString
);
403 foreach (Match m
in mc
)
405 note
.Add("<span class='note'><!--"
406 + m
.Groups
[1].Value
.Replace("<", "<") + "--></span>");
407 codeString
= regex
.Replace(codeString
,
408 "__Comments" + blockIndex
+ "__", 1);
413 #region Code which contained in <%%> tags
414 regex
= ((RegexStruct
)regExp
.regexStructList
[3]).regex
;
415 mc
= regex
.Matches(codeString
);
416 foreach (Match m
in mc
)
420 blockIndex
= vb
.Count
;
421 vb
.Add("<span class='declare'><%</span>" +
422 (m
.Groups
[1].Value
.Trim() != "" ?
423 HighlightCode(m
.Groups
[1].Value
, "vbs",
424 (RegExp
)hashTable
["vbs"]) : "")
425 + "<span class='declare'>%></span>");
429 blockIndex
= cs
.Count
;
430 cs
.Add("<span class='declare'><%</span>" +
431 (m
.Groups
[1].Value
.Trim() != "" ?
432 HighlightCode(m
.Groups
[1].Value
, "cs",
433 (RegExp
)hashTable
["cs"]) : "")
434 + "<span class='declare'>%></span>");
436 codeString
= regex
.Replace(codeString
,
437 "__" + lang
+ blockIndex
+ "__", 1);
441 #region Replace '&' character
442 codeString
= ((RegexStruct
)regExp
.regexStructList
[6]).regex
443 .Replace(codeString
, "&$1");
447 codeString
= ((RegexStruct
)regExp
.regexStructList
[7]).regex
448 .Replace(codeString
, RetrieveBrackets
);
449 codeString
= ((RegexStruct
)regExp
.regexStructList
[8]).regex
450 .Replace(codeString
, RetrieveBrackets
);
451 codeString
= htmlR
.Replace(codeString
, HTMLEval
);
454 #region Replace the string back to original value.
458 for (i
= 0; i
< note
.Count
; i
++) codeString
= codeString
.Replace("__Comments"
459 + i
+ "__", note
[i
].ToString());
460 codeString
= codeString
.Replace("__CharactersQuotes__", "\\\"").
461 Replace("__CharactersSingleQuote__", "\\'");
464 for (i
= 0; i
< css
.Count
; i
++) codeString
= codeString
.Replace("__CSS"
465 + i
+ "__", css
[i
].ToString());
468 for (i
= 0; i
< cs
.Count
; i
++) codeString
= codeString
.Replace("__C#"
469 + i
+ "__", cs
[i
].ToString());
471 // VBScript language or vb language.
472 for (i
= 0; i
< vb
.Count
; i
++) codeString
= codeString
.Replace("__VB"
473 + i
+ "__", vb
[i
].ToString());
475 // Javascript language.
476 for (i
= 0; i
< js
.Count
; i
++) codeString
= codeString
.Replace("__JS"
477 + i
+ "__", js
[i
].ToString());
485 /// Highlight the code depend on the language(except HTML language).
487 /// <param name="codeString">The string of code</param>
488 /// <param name="language">The language of code</param>
489 /// <param name="regExp">The regular expressions object</param>
490 /// <returns>Highlighted code</returns>
491 public static string HighlightCode(string codeString
,
492 string language
, RegExp regExp
)
495 language
= language
.ToLower();
496 codeString
= codeString
.Replace("<!--", "<!--");
497 RegexStruct regexStruct
;
498 ArrayList styleString
= new ArrayList(),
499 note
= new ArrayList(),
500 xmlnote
= new ArrayList();
504 #region Characters replaced
505 if (language
!= "css")
506 codeString
= codeString
.Replace("\\\"", "__CharactersQuotes__")
507 .Replace("\\'", "__CharactersSingleQuote__");
510 #region String replaced
511 if (language
!= "css")
513 regexStruct
= (RegexStruct
)regExp
.regexStructList
[0];
514 mc
= regexStruct
.regex
.Matches(codeString
);
515 foreach (Match m
in mc
)
517 styleString
.Add("<span class='" + regexStruct
.styleObject
+ "'>"
518 + m
.Groups
[0].Value
.Replace("<", "<")
520 codeString
= regexStruct
.regex
.Replace(codeString
,
521 "__StringVariables"+ blockIndex
+ "__", 1);
527 #region XML Comments for C# language replaced
529 if (language
== "cs")
531 Regex regex
= new Regex(@"((?<!/)///(?!/))([^\r\n]*)?"),
532 attri
= new Regex(@"(<[^>]+>)");
533 mc
= regex
.Matches(codeString
);
535 foreach (Match m
in mc
)
537 tmp
= m
.Groups
[2].Value
;
538 tmp
= attri
.Replace(tmp
, NoteBrackets
);
539 xmlnote
.Add("<span class='note'>"
540 + "<span class='gray'>///</span>"
542 codeString
= regex
.Replace(codeString
,
543 "__XMLComments" + blockIndex
+ "__", 1);
549 #region Comments replaced
550 regexStruct
= (RegexStruct
)regExp
.regexStructList
[language
== "css" ? 0 : 1];
551 mc
= regexStruct
.regex
.Matches(codeString
);
553 foreach (Match m
in mc
)
555 note
.Add("<span class='" + regexStruct
.styleObject
+ "'>"
556 + m
.Groups
[0].Value
.Replace("<", "<")
557 .Replace(">", ">")
559 codeString
= regexStruct
.regex
.Replace(codeString
,
560 "__Comments" + blockIndex
+ "__", 1);
565 #region other replaced
566 int i
= language
== "css" ? 1 : 2;
567 for (; i
< regExp
.regexStructList
.Count
; i
++)
569 regexStruct
= (RegexStruct
)regExp
.regexStructList
[i
];
570 if (language
== "cs" && regexStruct
.styleObject
== "Var")
571 codeString
= regexStruct
.regex
.Replace(codeString
,
572 "<span class='Var'>$1$2$3</span>");
574 codeString
= regexStruct
.regex
.Replace(codeString
, "<span class='"
575 + regexStruct
.styleObject
+ "'>$1</span>");
580 #region Replace the string back to original value.
581 if (language
!= "css") for (i
= 0; i
< styleString
.Count
; i
++)
582 codeString
= codeString
.Replace("__StringVariables" +
583 i
+ "__", styleString
[i
].ToString());
584 if (language
== "cs") for (i
= 0; i
< xmlnote
.Count
; i
++)
585 codeString
= codeString
.Replace("__XMLComments" +
586 i
+ "__", xmlnote
[i
].ToString());
587 for (i
= 0; i
< note
.Count
; i
++)
588 codeString
= codeString
.Replace("__Comments"
589 + i
+ "__", note
[i
].ToString());
590 if (language
!= "css")
592 // Replace the string which contains comments.
593 if (codeString
.IndexOf("__XMLComments") != -1)
594 for (i
= 0; i
< styleString
.Count
; i
++)
595 for (i
= 0; i
< xmlnote
.Count
; i
++)
596 codeString
= codeString
.Replace("__XMLComments" + i
597 + "__",ClearHTMLTag(xmlnote
[i
].ToString()));
598 if (codeString
.IndexOf("__Comments") != -1)
599 for (i
= 0; i
< styleString
.Count
; i
++)
600 for (i
= 0; i
< note
.Count
; i
++)
601 codeString
= codeString
.Replace("__Comments" + i
602 + "__",ClearHTMLTag(note
[i
].ToString()));
603 if (codeString
.IndexOf("__StringVariables") != -1)
604 for (i
= 0; i
< styleString
.Count
; i
++)
605 codeString
= codeString
.Replace("__StringVariables" + i
606 + "__", ClearHTMLTag(styleString
[i
].ToString()));
608 if (codeString
.IndexOf("__XMLComments") != -1)
609 for (i
= 0; i
< xmlnote
.Count
; i
++)
610 codeString
= codeString
.Replace("__XMLComments" + i
611 + "__", xmlnote
[i
].ToString());
612 codeString
= codeString
.Replace("__CharactersQuotes__", "\\\"")
613 .Replace("__CharactersSingleQuote__", "\\'");
621 /// Clear the HTML tags.
623 public static string ClearHTMLTag(string htmlString
)
625 // Clear the script tags.
626 htmlString
= Regex
.Replace(htmlString
,
627 @"<script[^>]*?>.*?</script>",
628 "", RegexOptions
.IgnoreCase
);
630 // Clear the HTML tags.
631 htmlString
= Regex
.Replace(htmlString
, @"<(.[^>]*)>",
632 "", RegexOptions
.IgnoreCase
);
633 htmlString
= Regex
.Replace(htmlString
, @"([\r\n])[\s]+",
634 "", RegexOptions
.IgnoreCase
);
635 htmlString
= Regex
.Replace(htmlString
, @"-->",
636 "", RegexOptions
.IgnoreCase
);
637 htmlString
= Regex
.Replace(htmlString
, @"<!--.*",
638 "", RegexOptions
.IgnoreCase
);
640 htmlString
= Regex
.Replace(htmlString
, @"&(quot|#34);",
641 "\"", RegexOptions
.IgnoreCase
);
642 htmlString
= Regex
.Replace(htmlString
, @"&(amp|#38);",
643 "&", RegexOptions
.IgnoreCase
);
644 htmlString
= Regex
.Replace(htmlString
, @"&(lt|#60);",
645 "<", RegexOptions
.IgnoreCase
);
646 htmlString
= Regex
.Replace(htmlString
, @"&(gt|#62);",
647 ">", RegexOptions
.IgnoreCase
);
648 htmlString
= Regex
.Replace(htmlString
, @"&(nbsp|#160);",
649 " ", RegexOptions
.IgnoreCase
);
650 htmlString
= Regex
.Replace(htmlString
, @"&(iexcl|#161);",
651 "\xa1", RegexOptions
.IgnoreCase
);
652 htmlString
= Regex
.Replace(htmlString
, @"&(cent|#162);",
653 "\xa2", RegexOptions
.IgnoreCase
);
654 htmlString
= Regex
.Replace(htmlString
, @"&(pound|#163);",
655 "\xa3", RegexOptions
.IgnoreCase
);
656 htmlString
= Regex
.Replace(htmlString
, @"&(copy|#169);",
657 "\xa9", RegexOptions
.IgnoreCase
);
658 htmlString
= Regex
.Replace(htmlString
, @"&#(\d+);",
659 "", RegexOptions
.IgnoreCase
);
661 htmlString
.Replace("<", "");
662 htmlString
.Replace(">", "");
663 htmlString
.Replace("\r\n", "");
664 htmlString
= HttpContext
.Current
.Server
665 .HtmlEncode(htmlString
).Trim();
670 /// Replace the properties of the THML tags.
672 /// <param name="m">Matching collection</param>
673 /// <returns>Replaced code</returns>
674 private static string HTMLEval(Match m
)
676 string tmp
= m
.Groups
[1].Value
;
677 if (tmp
.StartsWith("/"))
678 return "<span class='kw'></<span class='str'>"
679 + tmp
.Substring(1) + "</span>></span>";
680 else if (new Regex(@"^([_0-9a-z]+)\s*\/$",
681 RegexOptions
.IgnoreCase
).IsMatch(tmp
))
682 return "<span class='kw'><<span class='str'>"
683 + tmp
.Substring(0, tmp
.Length
- 1)
684 + "</span>></span>";
685 else if (tmp
.ToLower().StartsWith("!doctype"))
687 tmp
= "<span class='kw'>" + m
.Groups
[0].Value
.Substring(1)
689 tmp
= new Regex(@"\b(html|public)\b",
690 RegexOptions
.IgnoreCase
).Replace(tmp
,
691 "<span class='sqlstr'>$1</span>");
692 return "<span class='kw'><!" + tmp
+ "></span>";
696 Regex regex
= new Regex("([a-z_][a-z_0-9\\.\\-]*)\\s*=\\s*\"([^\"]*)\"",
697 RegexOptions
.IgnoreCase
);
698 tmp
= regex
.Replace(tmp
,
699 "<span class=\"sqlstr\">$1</span><span class=\"kw\">=\"$2\"</span>");
700 regex
= new Regex("([a-z_][a-z_0-9\\.\\-]*)\\s*=\\s*'([^']*)'",
701 RegexOptions
.IgnoreCase
);
702 tmp
= regex
.Replace(tmp
,
703 "<span class=\"sqlstr\">$1</span><span class=\"kw\">='$2'</span>");
704 regex
= new Regex("([a-z_][a-z_0-9\\-]*)\\s*=\\s*(?!['\"])(\\w+)",
705 RegexOptions
.IgnoreCase
);
706 tmp
= regex
.Replace(tmp
,
707 "<span class=\"sqlstr\">$1</span><span class=\"kw\">=$2</span>");
709 regex
= new Regex(@"^([a-z_0-9\-]+)", RegexOptions
.IgnoreCase
);
710 tmp
= regex
.Replace(tmp
, "<span class='str'>$1</span>");
711 if (tmp
.StartsWith("%@"))
712 return "<span class='str'><span class='declare'><%</span>"
713 + "<span class='kw'>@</span>"
714 + tmp
.Trim(new char[] { '%', '@' }
)
715 + "<span class='declare'>%></span></span>";
716 return "<span class='kw'><" + tmp
+ "></span>";
720 /// Replace the \r, \n to <br/>.
722 /// <param name="codeString"></param>
723 /// <returns></returns>
724 public static string Encode(string codeString
)
726 codeString
= codeString
.Replace("\r", "").Replace("\n", "<br/>");
727 return Regex
.Replace(codeString
, "(?<!<span)( +)(?!class)",
728 GetSpace
, RegexOptions
.Compiled
);
732 /// Replace the space to .
734 /// <param name="m"></param>
735 /// <returns></returns>
736 public static string GetSpace(Match m
)
738 return m
.Groups
[1].Value
.Replace(" ", " ");